
package com.aiYouPms.admin.service.impl;

import com.aiYouPms.admin.dao.UserDao;
import com.aiYouPms.admin.model.User;
import com.aiYouPms.admin.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    private final UserDao userMapper;

    public UserServiceImpl(UserDao userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public List<User> findAll() {
        return userMapper.findAll();
    }

    @Override
    public User findById(Long id) {
        return userMapper.findById(id);
    }

    @Override
    public void save(User user) {
        if (user.getId() == null) {
            userMapper.insert(user);
        } else {
            userMapper.update(user);
        }
    }

    @Override
    public void deleteById(Long id) {
        userMapper.deleteById(id);
    }

    @Override
    public List<User> findByName(String name) {
        return userMapper.findByName(name);
    }

    @Override
    public PageInfo<User> search(Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);

        return new PageInfo<>(userMapper.search(pageSize, pageNum));
    }
}